/**
 * 
 */
package com.quanyan.place.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.quanyan.common.utils.ExcelUtils;
import com.quanyan.place.entity.CiCardSalePrice;
import com.quanyan.place.entity.PlaceOrderItemSalePrice;
import com.quanyan.place.mapper.UpdatePlaceOrderItemSalePriceMapper;
import com.quanyan.place.service.UpdateItemSalePriceService;

/**
 *
 * @author tim
 * @time 2017年3月15日
 *
 */
@Service("updateItemSalePriceService")
public class UpdateItemSalePriceServiceImpl implements UpdateItemSalePriceService {
	private static Logger logger = LoggerFactory.getLogger(UpdateItemSalePriceServiceImpl.class);

//	@Value("${place.exportexcel.pricesale.url}")
	private String pricesaleUrl;
	@Autowired
	UpdatePlaceOrderItemSalePriceMapper placeOrderItemSalePriceMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.quanyan.place.service.UpdateItemSalePriceService#
	 * updatePlaceOrderItemSalePrice()
	 */
	@Override
	public void updatePlaceOrderItemSalePrice() {
		// TODO Auto-generated method stub

		List<CiCardSalePrice> ciCardSalePrices = placeOrderItemSalePriceMapper.getCiCardSalePrice();
		if (ciCardSalePrices != null && (!ciCardSalePrices.isEmpty())) {

			List<PlaceOrderItemSalePrice> placeOrderItems = new ArrayList<>();
			Map<String, Object> params = new HashMap<>();
			List<PlaceOrderItemSalePrice> placeOrderItemTmp = null;
			for (CiCardSalePrice ciCardSalePrice : ciCardSalePrices) {
				params.put("salePrice", ciCardSalePrice.getSalePrice());
				placeOrderItemTmp = placeOrderItemSalePriceMapper
						.backUPCiCardSalePrice(ciCardSalePrice.getPlaceOrderId());//获取待备份数据

				if (placeOrderItemTmp != null && (!placeOrderItemTmp.isEmpty())) {
					placeOrderItems.addAll(placeOrderItemTmp);
					if (placeOrderItems.size() > 0) {
						ExcelUtils.exportToExcelFile(placeOrderItems, pricesaleUrl);// 备份历史次卡数据到excel
						logger.info("批量备份更新数据成功");

						for (PlaceOrderItemSalePrice salePrice : placeOrderItemTmp) {
							params.put("id", salePrice.getId());
							placeOrderItemSalePriceMapper.batchUpdateOrderItemSalePrice(params);// 修改Saleprice
							logger.info("批量更新数据成功");
						}
						
					} else {
						logger.info("无批量更新数据备份");
					}

				}

			}

		}

	}

}
